package com.report.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.operator.Contain;
import cn.zhxu.bs.operator.Equal;
import cn.zhxu.bs.util.MapUtils;
import com.report.dto.request.MemberRuleReq;
import com.report.dto.response.MemberRuleRes;
import com.report.entity.Rule;
import com.report.mapper.MemberRuleMapper;
import com.report.service.IMemberRuleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author summer
 * @since 2024/06/26 23:21:57
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class MemberRuleServiceImpl extends ServiceImpl<MemberRuleMapper, Rule> implements IMemberRuleService {

    private final BeanSearcher beanSearcher;

    @Override
    public Page<MemberRuleRes> lists(MemberRuleReq memberRuleReq) {
        Page<MemberRuleRes> page = new Page<>(memberRuleReq.getPageNum(), memberRuleReq.getPageSize());
        Map<String, Object> build = MapUtils.builder().page(memberRuleReq.getPageNum()-1,memberRuleReq.getPageSize()).field(MemberRuleRes::getName, memberRuleReq.getName()).op(Contain.class).build();
        SearchResult<MemberRuleRes> search = beanSearcher.search(MemberRuleRes.class, build);
        page.setTotal(search.getTotalCount().longValue());
        page.setRecords(search.getDataList());
        return page;
    }

    @Override
    public MemberRuleRes findById(Integer id) {
        Map<String, Object> build = MapUtils.builder().field(MemberRuleRes::getId,id).op(Equal.class).build();
        return beanSearcher.searchFirst(MemberRuleRes.class,build);
    }
}
